package basis.第十四章;


import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Example3 {
    static int counter = 0;
    static Object obj = new Object();
    public static void main(String[] args) throws Exception {
        Runnable r1 = ()->{
            synchronized (obj){
                for (int i = 0; i < 5000; i++) {
                    counter++;
                }
            }
        };

        Runnable r2 = ()->{
            synchronized (obj){
                for (int i = 0; i < 5000; i++) {
                    counter--;
                }
            }
        };

        ExecutorService es = Executors.newFixedThreadPool(2);
        es.execute(r1);
        es.execute(r2);
        es.shutdown();
        Thread.sleep(5000);
        System.out.println(counter);
    }
}
